<?php
// application/forms/Article.php

class Form_NewBlock extends Zend_Form
{

    protected $_noDecorator = array(
        'ViewHelper', 
        'Errors'
    );

    public function cluBuild($data = null)
    {
        // set the method for the display form to POST
        $this->setAttrib('id', 'new_block');
        
        $this->setMethod('POST');
        $this->setAction('/en/block/index/'.$data['action']);
	        
        $urlValidator = new Zend_Validate_Regex('/^[A-Z0-9_\s\-\/\%]*$/i'); //alpha, num, space, -, _
        $myValidator = new Zend_Validate_Regex('/^[A-Z0-9_\s\-]*$/i'); //alpha, num, space, -, _
        $myValidator->setMessage(__("Only alphanum, spaces, -, _ allowed"), Zend_Validate_Regex::NOT_MATCH);
        $this->addElementPrefixPath('Clu_Filter', 'Clu/Filter/', 'filter');
                
        //add block name
        $name = new Zend_Form_Element_Text('name', 
                         array('disableLoadDefaultDecorators' => true,
                               'required'   => true,             
                               'class' => 'inputText'));
        
        $name->addDecorators($this->_noDecorator)
              ->addValidator($myValidator);
        $this->addElement($name);
        
        //add block name
        $title = new Zend_Form_Element_Text('title', 
                         array('disableLoadDefaultDecorators' => true,
                               'required'   => true,             
                               'class' => 'inputText'));
        
        $title->addDecorators($this->_noDecorator)
              ->addValidator($myValidator);
        $this->addElement($title);
        
        //text type
        $textType = new Zend_Form_Element_Select('text_type', 
                         array('disableLoadDefaultDecorators' => true,
                               'required'   => true));
        
        $textType->addMultiOption('html',__('HTML'))
               ->addMultiOption('php',__('PHP'))
               ->addDecorators($this->_noDecorator);
        $this->addElement($textType);
        
        //add description $user
        $html = new Clu_Form_Element_FckEditor('text', array(
        		'fckOptions' => array (
        						'Height' => 450,
        						'Width'  => 400,
        						'ToolbarSet' => 'Block'
        					)
       			 )
        );
        $html->setLabel(__('Text'));

		
        $this->addElement($html);
        
        
        //add action url
        $action_url = new Zend_Form_Element_Text('action_url', 
                         array('disableLoadDefaultDecorators' => true,
                               'class' => 'inputText'));
        
        $action_url->addDecorators($this->_noDecorator)
              ->addValidator($urlValidator);
        $this->addElement($action_url);
        
        
        //add type
        $type = new Zend_Form_Element_Select('type', 
                         array('disableLoadDefaultDecorators' => true,
                               'required'   => true));
        
        $type->addMultiOption('menu',__('Menu'))
               ->addMultiOption('ad',__('Ad'))
               ->addDecorators($this->_noDecorator);
        $this->addElement($type);
        
        //add linkto url
        $url = new Zend_Form_Element_Text('link_to', 
                         array('disableLoadDefaultDecorators' => true,
                               'class' => 'inputText'));
        $url->addDecorators($this->_noDecorator)
              /*->addValidator($myValidator)*/;
        $this->addElement($url);
        
        
 		//Applicable to which language
 		$languages = new Zend_Form_Element_MultiCheckbox('languages',  
                         array('disableLoadDefaultDecorators' => true));
        $languages->addMultiOption('all',__('All'))
        		->addMultiOption('vi',__('Vietnamese'))
               ->addMultiOption('en',__('English'))
               ->addMultiOption('cn',__('Chinese'))
               ->addMultiOption('jp',__('Japanese'))
               ->addDecorators($this->_noDecorator);
        $this->addElement($languages);
        
        //user-configurable?
        $userConfig = new Zend_Form_Element_Select('user_configurable', 
                         array('disableLoadDefaultDecorators' => true,
                               'required'   => true));
        
        $userConfig->addMultiOption('0',__('No'))
               ->addMultiOption('1',__('Yes'))
               ->addDecorators($this->_noDecorator);
        $this->addElement($userConfig);
        
        //shared owner
        $owner = new Zend_Form_Element_Text('owner', 
                         array('disableLoadDefaultDecorators' => true,
                               'class' => 'inputText'));
        
        $owner->addDecorators($this->_noDecorator);
//              ->addValidator($urlValidator);
        $this->addElement($owner);
        
        
 		//Applicable to which language
 		$theme = new Zend_Form_Element_MultiCheckbox('themes',  
                         array('disableLoadDefaultDecorators' => true));
        $theme->addMultiOption('all',__('All'));
        
        foreach (explode (' ',Zend_Registry::get('configuration')->themes) as $v)
	        $theme->addMultiOption($v, $v);
        $theme->addDecorators($this->_noDecorator);
        $this->addElement($theme);
        
        //config by modules
        $moduleList = Zend_Registry::get('configuration')->modules;
     	$modules = new Zend_Form_Element_MultiCheckbox('modules',  
                             array('disableLoadDefaultDecorators' => true));
        $modules->addMultiOption('all','All');
        foreach ($moduleList as $module=>$actionList)
        {
        	//TODO: for now , just set to module level
	        $modules->addMultiOption($module,$module);
	            
            /*	
			$actions = explode (',', $actionList);
//			die_a($actions);                           
			foreach ($actions as $a)                             
	            $setting->addMultiOption($module . '_' . $a,__($a . ' ' . $module));
			*/
        }
        $modules->addDecorators($this->_noDecorator);
        $this->addElement($modules);
        
        // config by user roles
		$db = Zend_Db_Table::getDefaultAdapter();
		$sql = "SELECT * FROM permissions";
		$permissions = $db->fetchAll($sql);
		$roles = new Zend_Form_Element_MultiCheckbox('roles',  
                             array('disableLoadDefaultDecorators' => true));
                             
		$roles->addMultiOption(0,__('Anonymous'));                             
		foreach ($permissions as $p)                             
	            $roles->addMultiOption($p['rid'],__($p['rname']));
			            
        $roles->addDecorators($this->_noDecorator);
        $this->addElement($roles);
		
        
        //themes
        $themes = new Zend_Form_Element_MultiCheckbox('themes',array('disableLoadDefaultDecorators' => true));
        $themes->addMultiOption('all',__('All'));
        foreach (explode (' ',Zend_Registry::get('configuration')->themes) as $v)
	        $themes->addMultiOption($v, $v);
        $themes->addDecorators($this->_noDecorator);
        $this->addElement($themes);
        
        //template
        $template = new Zend_Form_Element_MultiCheckbox('template',array('disableLoadDefaultDecorators' => true));
        $template->addMultiOption('all',__('All'));
        foreach (explode(' ', Zend_Registry::get('configuration')->templates) as $k => $v)
	        $template->addMultiOption($v, $v);
        $template->addDecorators($this->_noDecorator);
        $this->addElement($template);
        
        //region
        $region = new Zend_Form_Element_Select('region',array('disableLoadDefaultDecorators' => true));
        $region->addMultiOption('all',__('All'));
        $region->addMultiOption('1', __('1'))
        	   ->addMultiOption('2', __('2'))
        	   ->addMultiOption('3', __('3'))
        	   ->addMultiOption('4', __('4'))
        	   ->addMultiOption('5', __('5'));
        $region->addDecorators($this->_noDecorator);
        $this->addElement($region);
        
        //weight
        $weight = new Zend_Form_Element_Select('weight', 
                         array('disableLoadDefaultDecorators' => true));
		$weight->addMultiOption('all', __('Select'))
			   ->addMultiOption('1', __('1'))
        	   ->addMultiOption('2', __('2'))
        	   ->addMultiOption('3', __('3'))
        	   ->addMultiOption('4', __('4'))
        	   ->addMultiOption('5', __('5'));
        $weight->addDecorators($this->_noDecorator); 
        $this->addElement($weight);
        
		//add the submit button
        $submit = new Zend_Form_Element_Submit('submit', array('disableLoadDefaultDecorators' => true, 
                                                               'label' => __('Submit') ));
        $submit->addDecorators($this->_noDecorator);
        $this->addElement($submit);
    }
    
    public function populateForm($data)
    {
		$this->populate($data);
    }
}
